package com.elitedatai.mchtest.media.controller;

import com.dji.sdk.cloudapi.media.*;
import com.dji.sdk.cloudapi.media.api.IHttpMediaService;
import com.dji.sdk.common.HttpResultResponse;
import com.elitedatai.mchtest.media.service.IMediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.util.List;

/**
 * 媒体控制器类
 */
@Slf4j
@RestController
public class MediaController implements IHttpMediaService {

    @Autowired
    private IMediaService mediaService;

    /**
     * 通过指纹检查文件是否已上传
     *
     * @param workspaceId 工作空间ID
     * @param request 包含文件指纹信息的请求对象
     * @param req HTTP请求对象
     * @param rsp HTTP响应对象
     * @return 如果文件存在返回成功响应，否则返回错误响应
     */
    @Override
    public HttpResultResponse mediaFastUpload(String workspaceId, @Valid MediaFastUploadRequest request, HttpServletRequest req, HttpServletResponse rsp) {
        boolean isExist = mediaService.fastUpload(workspaceId, request.getFingerprint());

        return isExist ? HttpResultResponse.success() : HttpResultResponse.error(request.getFingerprint() + "don't exist.");
    }

    /**
     * 当文件通过pilot上传到存储服务器时，通过此接口上报文件的基本信息
     *
     * @param workspaceId 工作空间ID
     * @param request 包含文件上传信息的请求对象
     * @param req HTTP请求对象
     * @param rsp HTTP响应对象
     * @return 返回包含对象键的成功响应
     */
    @Override
    public HttpResultResponse<String> mediaUploadCallback(String workspaceId, @Valid MediaUploadCallbackRequest request, HttpServletRequest req, HttpServletResponse rsp) {
        mediaService.saveMediaFile(workspaceId, request);
        return HttpResultResponse.success(request.getObjectKey());
    }

    /**
     * 根据工作空间ID和微指纹集合查询此工作空间中已存在的文件
     *
     * @param workspaceId 工作空间ID
     * @param request 包含微指纹列表的请求对象，请求体中只有一个tiny_fingerprint参数，
     *                但不建议使用Map来接收参数
     * @param req HTTP请求对象
     * @param rsp HTTP响应对象
     * @return 返回包含已存在微指纹列表的响应对象
     */
    @Override
    public HttpResultResponse<GetFileFingerprintResponse> getExistFileTinyFingerprint(String workspaceId, @Valid GetFileFingerprintRequest request, HttpServletRequest req, HttpServletResponse rsp) {
        List<String> existingList = mediaService.getExistTinyFingerprints(workspaceId, request.getTinyFingerprints());
        return HttpResultResponse.success(new GetFileFingerprintResponse().setTinyFingerprints(existingList));
    }

    /**
     * 文件夹上传回调方法
     *
     * @param workspaceId 工作空间ID
     * @param request 文件夹上传回调请求对象
     * @param req HTTP请求对象
     * @param rsp HTTP响应对象
     * @return 返回空响应
     */
    @Override
    public HttpResultResponse folderUploadCallback(String workspaceId, @Valid FolderUploadCallbackRequest request, HttpServletRequest req, HttpServletResponse rsp) {
        return null;
    }
}